/**
 * Project Name:youba
 * Package Name:com.inoneone.youba.mcht.conf
 * Date:2017/7/19下午6:43
 * Copyright (c) 2017, www.zbj.com All Rights Reserved
 */
package com.inoneone.youba.mcht.job;

import com.inoneone.youba.mcht.dao.mapper.ExtendOrderMapper;
import com.inoneone.youba.mcht.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * Description: (描述) </br>
 * Date: 2017/7/19下午6:43 </br>
 *
 * @author 姬远玄
 * @since JDK 1.7
 */

@Service
public class CollectStatistics {
    @Autowired
    private ExtendOrderMapper extendOrderMapper;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Scheduled(cron = "0 0 2 * * ?") // 每天凌晨2点，统计昨天的数据
    public void collect() {

        logger.info("开始执行数据统计");
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(new Date());
        calendarStart.add(Calendar.DAY_OF_MONTH, -1);
        calendarStart.add(Calendar.MONTH, -1);

        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.setTime(new Date());

        while (calendarStart.getTime().getTime() < calendarEnd.getTime().getTime()) {

            collect(calendarStart.getTime());
            calendarStart.add(Calendar.DAY_OF_MONTH, 1);
        }
    }

    private void collect(Date date) {
        String dateStr = DateUtil.formatDate(date, "yyyy-MM-dd");
        try {
            extendOrderMapper.collectCardFuel(dateStr);
            logger.info("统计ic卡维度加油数据成功," + dateStr);
        } catch (Exception e) {
            logger.debug("统计ic卡维度加油数据失败," + dateStr, e);
        }

        try {
            extendOrderMapper.collectMchFuel(dateStr);
            logger.info("统计商户维度加油数据成功," + dateStr);
        } catch (Exception e) {
            logger.debug("统计商户维度加油数据失败," + dateStr, e);
        }

        try {

            extendOrderMapper.collectTankerFuel(dateStr);
            logger.info("统计加油机维度加油数据成功," + dateStr);
        } catch (Exception e) {
            logger.debug("统计加油机维度加油数据失败," + dateStr, e);

        }


        try {

            extendOrderMapper.collectCardCharge(dateStr);
            logger.info("统计ic卡维度充值数据成功," + dateStr);
        } catch (Exception e) {
            logger.debug("统计ic卡维度充值数据失败," + dateStr, e);

        }

        try {

            extendOrderMapper.collectMchCardCharge(dateStr);
            logger.info("统计商户维度充值数据成功," + dateStr);
        } catch (Exception e) {
            logger.debug("统计商户维度充值数据失败," + dateStr, e);

        }
    }
}
